package com.miukoo.aikao.qpaper.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miukoo.aikao.core.dtos.PageReqDto;
import com.miukoo.aikao.core.dtos.PageResDto;
import com.miukoo.aikao.core.utils.BeanMapperUtils;
import com.miukoo.aikao.qpaper.dto.PaperQuestionDto;
import com.miukoo.aikao.qpaper.dto.ext.PaperQuestionDetailDto;
import com.miukoo.aikao.qpaper.mapper.PaperQuestionMapper;
import com.miukoo.aikao.qpaper.pojos.PaperQuestion;
import com.miukoo.aikao.qpaper.service.PaperQuestionService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* <p>
* 语言设置 服务实现类
* </p>
*
* @author 聪明笨狗
* @since 2020-05-25 16:33
*/
@Service
public class PaperQuestionServiceImpl extends ServiceImpl<PaperQuestionMapper, PaperQuestion> implements PaperQuestionService {

    @Override
    public PageResDto<List<PaperQuestion>> paging(PageReqDto<PaperQuestionDto> reqDto) {

        //创建分页对象
        IPage<PaperQuestion> query = new Page<>(reqDto.getCurrent(), reqDto.getSize());

        //查询条件
        QueryWrapper<PaperQuestion> wrapper = new QueryWrapper<>();

        //获得数据
        IPage<PaperQuestion> page = this.page(query, wrapper);
        return PageResDto.result(page.getTotal(),page.getRecords());
     }

    @Override
    public List<PaperQuestionDto> listByPaper(Long paperId) {

        //查询条件
        QueryWrapper<PaperQuestion> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(PaperQuestion::getPaperId, paperId)
                .orderByAsc(PaperQuestion::getSort);

        List<PaperQuestion> list = this.list(wrapper);
        return BeanMapperUtils.mapList(list, PaperQuestionDto.class);
    }

    @Override
    public PaperQuestion findByKey(Long paperId, Long quId) {
        //查询条件
        QueryWrapper<PaperQuestion> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(PaperQuestion::getPaperId, paperId)
                .eq(PaperQuestion::getQuestionId, quId);

        return this.getOne(wrapper, false);
    }

    @Override
    public void updateByKey(PaperQuestion qu) {

        //查询条件
        QueryWrapper<PaperQuestion> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(PaperQuestion::getPaperId, qu.getPaperId())
                .eq(PaperQuestion::getQuestionId, qu.getQuestionId());

        this.update(qu, wrapper);
    }

    @Override
    public int sumRadioScore(Long paperId) {
        return baseMapper.sumRadioScore(paperId);
    }

    @Override
    public int sumQaScore(Long paperId) {
        return baseMapper.sumQaScore(paperId);
    }

    @Override
    public List<PaperQuestionDetailDto> listForPaperResult(Long paperId) {
        return baseMapper.listByPaper(paperId);
    }
}
